<template>
  <div class="login">
    <!-- 登录页 -->
    <!--  $router.back()  返回上一个路径 -->
    <van-nav-bar title="账号登录" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' },
        { pattern: /^\w{5,}$/,message: '长度为5到8位,只能出现数字、字母、下划线'}]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' },
        { pattern: /^\w{5,}$/,message: '长度为5到8位,只能出现数字、字母、下划线'}]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <div class="footer">
        <router-link class="btn" to="/register">还没有账号，去注册~</router-link>
    </div>
  </div>
</template>

<script>
import { loginApi } from '@/api/user'
import { setItem } from '@/utils/storage'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await loginApi(values)
      // 保存token
      setItem(res.data.body.token)
      // 登录成功提示
      this.$toast.success(res.data.description)
      this.$router.push('/home')
    }
  }
}
</script>

<style lang="less" scoped>
.footer {
    font-size: 14px;
    color: #666;
    text-align: center;
    .btn {
    color: #666;
  }
}
</style>
